package Server;
import Server.Bean.Express;
import Server.Dao.ExpressDao;
import Server.View.View;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

public class Server {
    private ServerSocket server;
    private final int PORT = 8080;
    static ExpressDao dao = new ExpressDao();
    static View v = new View();
    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }
    public void start(){
        int i = 0;
        try {
            server = new ServerSocket(PORT);
            System.out.println("服务器已启动");
            while (true) {
                i++;
                Socket socket = server.accept();
                System.out.println("====已经有" + i + "个用户登录====");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        login(socket);
                        try {
                            //将数据存储到文件中
                            /*dao.adminWrite();
                            dao.userWrite();*/
                            dao.expressWrite();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (server != null) {
                    server.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void login(Socket socket) {
        InputStream is = null;
        OutputStream os = null;
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        try {
            is = socket.getInputStream();
            os = socket.getOutputStream();
            ois = new ObjectInputStream(is);
            oos = new ObjectOutputStream(os);
            w:
            while (true){
                char flag = ois.readChar();
                User u = (User) ois.readObject();
                if (flag=='1') {//传入1时代表用户身份是管理员
                    if(dao.adminFindByNameAndByPwd(u)){//判断管理员用户账号密码是否正确
                        //正确把ture传到客户端
                        oos.writeObject(true);
                        oos.flush();
                        while (true) {
                            int mune = (int) ois.readObject();
                            if(mune==0){
                                break;
                            }
                            cClient(mune,oos,ois);
                        }
                        break;
                    }else{
                        //错误把false传到客户端
                        oos.writeObject(false);
                        oos.flush();
                        continue;
                    }
                }else if(flag=='2'){
                    if(dao.userFindByNameAndByPwd(u)){
                        //正确把ture传到客户端
                        oos.writeObject(true);
                        oos.flush();
                        while(true){
                            Boolean b = uClient(oos, ois);
                            if(b==true){
                                break w;
                            }
                        }
                    }else{
                        //错误把false传到客户端
                        oos.writeObject(false);
                        oos.flush();
                        continue;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private static Boolean uClient(ObjectOutputStream oos,ObjectInputStream ois) throws IOException, ClassNotFoundException {
        //1.    获取用户输入的取件码
        int s = (int) ois.readObject();
        //2.    查找快递是否存在
        Express e = dao.findBycode(s);
        //3.    将查找结果返回客户端:存在返回快递对象，不存在返回null
        oos.writeObject(e);
        oos.flush();
        if(e!=null){
            dao.delete(e.getNumber());
            return true;
        }
        return false;
    }
    public static void cClient(int mune,ObjectOutputStream oos,ObjectInputStream ois) throws Exception {
        switch (mune){
            case 0:
                return;
            case 1:{//快递录入
                System.out.println(1);
                //1.    提示输入快递信息
                Express e = (Express) ois.readObject();
                //2.    查询单号是否已经存在
                Express e2 = dao.findBynumber(e.getNumber());
                if(e2==null){
                    //3.    存储快递
                    dao.add(e);
                    oos.writeObject(true);
                    oos.writeObject(e);
                    oos.flush();
                }else {
                    oos.writeObject(false);
                    oos.flush();
                }
                break;
            }
            case 2:{//快递修改
                System.out.println(2);
                //1.    接受要修改的快递的单号
                String number = (String) ois.readObject();
                //2.    查找快递是否存在
                Express e = dao.findBynumber(number);
                if(e==null){
                    oos.writeObject(false);
                    oos.flush();
                }else {
                    oos.writeObject(true);
                    oos.writeObject(e);
                    oos.flush();
                    Express newe = (Express) ois.readObject();
                    if(dao.findBynumber(newe.getNumber())==null){
                        Express e2 = dao.update(e.getNumber(), newe);
                        oos.writeObject(e2);
                    } else{
                        oos.writeObject(null);
                    }
                    oos.flush();
                }
            }
            break;
            case 3:{//删除
                System.out.println(3);
                //1.    输入快递单号
                String number = (String) ois.readObject();
                //2.    查找快递是否存在
                Express e = dao.findBynumber(number);
                if(e==null){//不存在返回null
                    oos.writeObject(false);
                    oos.flush();
                }else {//存在将ture和快递对象返回
                    oos.writeObject(true);
                    oos.writeObject(e);
                    oos.flush();
                    int type = (int) ois.readObject();
                    if(type == 1){
                        dao.delete(number);
                    }
                }
            }
            break;
            case 4:{//客户端想查看所有快递
                //将存储快递对象的HashMap返回给客户端
                System.out.println(4);
                HashMap<String,Express> data = dao.findall();
                Collection<Express> values = data.values();
                ArrayList<Express> expresses1 = new ArrayList<>(values);
                Express[] expresses=new Express[expresses1.size()];
                data.values().toArray(expresses);
                oos.writeObject(expresses);
                oos.flush();
            }
            break;
        }
    }
}

